<?php

namespace app\api\controller;

use app\common\controller\Logined;
use think\Db;
use app\common\service\InvestService;
use app\common\service\UserService as User;

class Team extends Logined{

    public function team(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }

        $uids = Db::table('tp_user')->where('pid',$uid)->select();
        $data = [];
        foreach ($uids as $key => $val) {
            $a = [];
            $a['username'] = $val['username'];
            $a['date']     = $val['reg_date'];
            $a['yj'] = Db::table('tp_invest_statistics')->where('uid',$val['id'])->sum('total');
            $data[] = $a;
        }
        $this->retData(0, 'success',$data); 

    }
    // public function team(){
    //     $uid = $this->uid;
    //     if (empty($uid)) {
    //         $this->retData(1, '缺少参数');
    //     }
    //     $uids = $this->getXids($uid);
    //     // if($uids['depth']==0){
    //     //     $this->retData(0, 'fail');
    //     // }
    //     $data          =  [];
    //     $data['depth'] = [];
    //     $data['num']   = count($uids['uids']);
    //     $data['total'] = 0;
    //     for ($i=1; $i < $uids['depth']+1; $i++) { 
    //         $d_num = count($uids['d'][$i]);
    //         $amount = Db::table('tp_invest_record')->where('status',1)->where('uid','in',$uids['d'][$i])->sum('amount');
    //         // if ($i <= 3) {
    //         //      $data['depth'][] = ['d'=>$i,'d_num'=>$d_num,'amount'=>$amount];
    //         // }
    //        $data['depth'][] = ['d'=>$i,'d_num'=>$d_num,'amount'=>$amount];
    //         $data['total'] += $amount;
    //     }
    //     $this->retData(0, 'success',$data); 
    // }

    // //没代详细
    // public function Ateam(){
    //     $uid   = $this->uid;
    //     $depth = P('depth');
    //     if (empty($uid) || empty($depth)) {
    //         $this->retData(1, '缺少参数');
    //     }
    //     $data = [];
    //     $uids = model('UserModel')->getOnePid($uid,$depth);
    //     // $uids = $uids['d'][$depth];
    //     foreach ($uids as $key => $val) {
    //         $phone = Db::table('tp_user')->where('id',$val)->value('phone');
    //         $amount = Db::table('tp_invest_record')->where('status',1)->where('uid',$val)->sum('amount');
    //         if (empty($phone)) {
    //             $phone = Db::table('tp_user')->where('id',$val)->value('mail');
    //         }
    //         $data[] = [
    //             'phone'=>substr_replace($phone, '****', 3, 4),
    //             'amount'=>$amount
    //         ];
    //     }
    //     $this->retData(0, 'success',$data);
    // }

    // private function getXids($uid){
    //     $data      = [];
    //     $data['d'] = [];
    //     $uids      = [];
    //     $i         = 0;
    //     while ($uid) {
    //         $uid = Db::table('tp_user')->where('pid','in',$uid)->column('id');
    //         if ($uid) {
    //             $uids = array_merge($uids,$uid);
    //             $i += 1;
    //              $data['d'][$i] = $uid;
    //         }
            
    //     }
    //     $data['uids']  = $uids;
    //     $data['depth'] = $i;
    //     return $data; 
    // }

    public function team1(){
        $uid = $this->uid;
        $depth = P('depth')?:1;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $data = [];
        
        $data['total_num']  = Db::table('tp_user_team')->where('uid',$uid)->count();
        $data['depth'] = Db::table('tp_user_team')
                            ->where('uid',$uid)
                            ->where('depth','<=',10)
                            ->group('depth')
                            ->field('count(*) num,depth')
                            ->select();                
        $this->retData(0,'success',$data);
    }

    public function team2(){
        $uid = $this->uid;
        $depth = P('depth')?:1;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $data = Db::table('tp_user_team')->alias('a')
                            ->join('tp_user b','b.id=a.x_uid')
                            ->where('a.uid',$uid)
                            ->where('depth',$depth)
                            ->field('username,robot_level,reg_date')
                            ->select();
        foreach ($data as $key => &$val) {
            if ($depth != 1) {
                $val['username'] = substr_replace($val['username'], '****', 3, 4);
            }
            
            $val['status'] = Db::table('tp_invest_record')->where('uid',$uid)->order('id desc')->value('status')?:0;
        }
        $this->retData(0,'success',$data);             
    }
}